import { getToken, setToken, removeToken } from '@/utils/auth'
import { loginFn } from '@/api/user'
const state = {
  token: getToken(),
  UserInfo: JSON.parse(localStorage.getItem('UserInfo'))
}
const mutations = {
  updataToken(state, token) {
    state.token = token
    setToken(token)
  },
  delToken(state) {
    state.token = null
    removeToken()
  },
  updatauserInfo(state, user) {
    state.UserInfo = user
    localStorage.setItem('UserInfo', JSON.stringify(user))
  }
}
const actions = {
  async login(context, data) {
    const res = await loginFn(data)
    console.log(res)
    context.commit('updatauserInfo', res)
    const { token } = await loginFn(data)
    console.log(token)
    await context.commit('updataToken', token)
  },
  logout({ commit }) {
    commit('delToken')
    commit('updatauserInfo', {})
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
